package com.yanqu.road.server.cmd.player;

import com.yanqu.road.entity.player.QueryPlayerResult;
import com.yanqu.road.entity.player.UserBadge;
import com.yanqu.road.entity.player.UserDecoration;
import com.yanqu.road.logic.pb.BadgePb;
import com.yanqu.road.logic.pb.DecorationPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.decoration.DecorationProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.S_QUERY_PLAYER, desc = "查看玩家")
public class QueryPlayerCmd extends AbstractCommand {

	private long watchUserId;

	private long watchServerId;

	private QueryPlayerResult queryPlayerResult;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		DecorationProto.QueryPlayerReqMsg reqMsg = DecorationProto.QueryPlayerReqMsg.parseFrom(packet.getBytes());
		watchUserId = reqMsg.getUserId();
		watchServerId = reqMsg.getServerId();
		if(0 == watchServerId){
			watchServerId = GameServer.getInstance().getServerId();
		}
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if(watchServerId == GameServer.getInstance().getServerId()) {
			if (player.getUserId() == watchUserId) {
				ret = GameErrorCode.E_QUERY_NO_SELF;
				return;
			}
			queryPlayerResult = UserMgr.queryPlayer(watchUserId);
		}else {
			DecorationProto.QueryPlayerReqMsg.Builder msg = DecorationProto.QueryPlayerReqMsg.newBuilder();
			msg.setUserId(watchUserId);
			msg.setServerId(watchServerId);
			YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_QUERY_PLAYER, msg);
			GamePlayerMgr.sendPacket(player.getUserId(), pbMsg);
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		if(watchServerId == GameServer.getInstance().getServerId()) {
			DecorationProto.QueryPlayerRespMsg.Builder respMsg = DecorationProto.QueryPlayerRespMsg.newBuilder();
			respMsg.setRet(ret);
			if (0 == ret) {
				respMsg.setRet(queryPlayerResult.getRet());
				if(0 == queryPlayerResult.getRet()){
					PlayerProto.PlayerSimpleTempMsg.Builder userMsg = PlayerPb.parsePlayerSimpleTempMsg(queryPlayerResult.getWatchUserInfo());
					if (null != userMsg) {
						respMsg.setUserData(userMsg);
					}
					if (null != queryPlayerResult.getDecorationMap()) {
						long nowTime = System.currentTimeMillis() / 1000;
						for (UserDecoration userDecoration : queryPlayerResult.getDecorationMap().values()) {
							if (userDecoration.getEndTime() > nowTime) {
								DecorationProto.DecorationTempMsg.Builder decorationMsg = DecorationPb.parseDecorationTempMsg(userDecoration);
								if (null != decorationMsg) {
									respMsg.addDecorationList(decorationMsg);
								}
							}
						}
					}
					if (null != queryPlayerResult.getUserBadgeList()) {
						for (UserBadge userBadge : queryPlayerResult.getUserBadgeList()) {
							respMsg.addBadge(BadgePb.parseBadgeTempMsg(userBadge));
						}
					}
					if (queryPlayerResult.getIpLocation() != null) {
						respMsg.setIpAddress(queryPlayerResult.getIpLocation());
					}
				}
			}
			player.sendPacket(Protocol.U_QUERY_PLAYER, respMsg);
		}
	}
}
